<?php
namespace Home\Controller;
use Think\Controller;

class OrderController extends \Common\Controller\UserController {
	//订单列表
    public function index() {
    	$data = I('get.');

        $search = '';

        $sqlw = ' 1 = 1 ';
        if(!empty($data['b']) && !empty($data['e'])) {
        	$b = strtotime($data['b']);
        	$e = strtotime($data['e']) + 24 * 60 * 60;
        } else if(!empty($data['b']) && $data['e'] == '') {
        	$b =  strtotime($data['b']);
        	$e = $b + 24 * 60 * 60;
        } else if($data['b'] == '' && !empty($data['e'])) {
        	$b = strtotime($data['e']);
        	$e =  $b + 24 * 60 * 60;
        } else {
        	$b = 0;
        	$e = 1999999999;
        }
		$sqlw .= ' AND createtime >= ' . $b . ' AND createtime < ' . $e;

        $data['b'] = date('Y-m-d', $b);
        $data['e'] = date('Y-m-d', $e);

        $this->assign('begintime', $data['b']);
        $this->assign('endtime', $data['e']);

		$search .= '&b=' . $data['b'];
		$search .= '&e=' . $data['e'];

        $sqlw .= ' AND userid = ' . session('userid');

		$other=empty($_REQUEST['o'])?0:$_REQUEST['o'];
        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;

        $room = M('order');
    	$info = $room
    	-> field('tm_order.id,tm_order.userid,tm_order.orderid,tm_order.productname,
    	tm_order.price, tm_order.number,tm_order.createtime,tm_order.status,
		tm_order_address.kdgs,tm_order_address.kddh')
    	-> join('tm_order_address on tm_order.id=tm_order_address.orderid')
    	-> order(' id desc') -> where($sqlw) -> limit(($page-1)*$pagesize,$pagesize) -> select();

        $query = $room->query('select count(*) as total from tm_order where ' . $sqlw);

        $pageurl='/user/order.html?';

        $count = $query[0]['total'];

        $this -> assign('room',$info);

        $this -> assign('total',$count);

        if($count%$pagesize==0){
        	$this->assign('count',$count/$pagesize);
        } else {
        	$this->assign('count',(int)floor($count/$pagesize)+1);
        }

        $this->assign('page', $page);

        $this->assign('search', $search);

        $this->assign('pageurl', $pageurl);
        $this->assign('pagesize', $pagesize);
        $this->display();

    }

    //订单详情
    public function detail() {
        $id=I('get.id');

        $this->assign('data',M('order')->where('id='.$id)->find());
        $this->assign('pdata',M('order_product')->where('orderid='.$id)->select());
        $this->assign('adata',M('order_address')->where('orderid='.$id)->find());

        $this->display();
    }
    
    // 确认收货
    public function comfirm() {
    	$id = I('get.id');
    	if(IS_POST) {
    		$User = M('user') -> field('paypassword') -> where('id = ' . session('userid')) -> find();
    		if(!(new \Think\PasswordHash()) -> CheckPassword(md5(I('post.paypassword')), $User['paypassword'])) {
    			echo"<script>alert('支付密码错误！');history.go(-1);</script>";
    			exit;
    		}

    		if(M('order') -> where('id = ' . $id) -> save(array('status' => 4))) {
    			echo"<script>alert('收货成功！');location.href='/user/order.html';</script>";
    			exit;
    		}
    	}

    	$this->assign('data',M('order')->where('id='.$id)->find());
        $this->assign('pdata',M('order_product')->where('orderid='.$id)->select());
        $this->assign('adata',M('order_address')->where('orderid='.$id)->find());

    	$this->display();
    }
}
?>